package com.jfinal.bbsapp.common.interceptor;

import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

public class TokenInterceptor implements Interceptor {
	

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		HttpServletRequest request = controller.getRequest();
		String token = request.getHeader("token");
		Cache res = Redis.use();
		Set<String> keys = res.keys("app-" + token + "-*");	
		if(token==null || token=="" || keys.size() <= 0) {
			controller.renderJson(Ret.fail("令牌失效，请重新登录").set("code",40000));
			return;
		}		
		//跳出
		inv.invoke();
	}
}
